/*    This program calculates the amount a customer should be
    charged at a carpet store
       Written by: Amrita Mukherjee
       Date: 4th May 2011
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define TAX_PERCENT 8.5

//    Function Declarations
int  getRandNum( int low, int high);

void getCarpetData (int*  sqrYards,
                    float* costSqrYard,
                    float* laborSqrYard,
                    float* prepCost,
                    float* discount);

void calcCosts (int    sqrYards,
                float  costSqrYard,
                float  laborSqrYard,
                float  prepCost,
                float  discount,
                float* carpetCost,
                float* laborCost,
                float* discountAmount);

void calcCharge  (float carpetCost,
                 float laborCost,
                 float discountAmount,
                 float* tax_carpet,
                 float* charge_customer); // parameter list must be written
void printHeader     (void);
void printCarpetData (int custNo,
                      int sqrYards,
                      float costSqrYard,
                      float laborSqrYard,
                      float prepCost,
                      float discount); // parameter list must be written
void printResults (float carpetCost,
                   float laborCost,
                   float discount,
                   float discountAmount,
                   float tax_carpet,
                   float charge_customer); // parameter list must be written
void printFooter (void);

int main (void)
{
// Local Declarations
    int    sqrYards;
    float  costSqrYard;
    float  laborSqrYard;
    float  prepCost;
    float  discount;
    
    float carpetCost;
    float laborCost;
    float discountAmount;
    float tax_carpet;
    float charge_customer;

    const int low  = 10000;
    const int high = 99999;
    int custNo;
    
// Statements
    custNo = getRandNum(low, high);

    getCarpetData (&sqrYards,
                   &costSqrYard,
                   &laborSqrYard,
                   &prepCost,
                   &discount);
    
    calcCosts (sqrYards,
               costSqrYard,
               laborSqrYard,
               prepCost,
               discount,
               &carpetCost,
               &laborCost,
               &discountAmount);

    calcCharge (carpetCost,
                laborCost,
                discountAmount,
                &tax_carpet,
                &charge_customer);

    printHeader ();

    printCarpetData (custNo,
                     sqrYards,
                     costSqrYard,
                     laborSqrYard,
                     prepCost,
                     discount);

    printResults (carpetCost,
                  laborCost,
                  discount,
                  discountAmount,
                  tax_carpet,
                  charge_customer);

    printFooter();
    
    return 0;
} // main

/*
====================================================
    This function generates a random number within
    the range low to high
       Pre :  low
              high
       Post:  random number within the specified range
              generated and returned
====================================================
*/
int  getRandNum( int low, int high)
{

//  Local Declarations
    int range; 
    int custNo;

//statements
    range = (high - low)+1;
    custNo = rand() % range + low;

    return custNo; // <-- this must be changed
}

/*
====================================================
    This function reads the user input into variables
    specified in the parameter list.
        Pre :  Nothing.
        Post:  Data read and placed in calling function.
====================================================
*/
void getCarpetData (int*   sqrYards,
                    float* costSqrYard,
                    float* laborSqrYard,
                    float* prepCost,
                    float* discount)
{
//    Statements
    printf("Square yards purchased: ");
    scanf("%d", sqrYards);
    printf("Cost per square yards : ");
    scanf("%f", costSqrYard);
    printf("Labor per square yards: ");
    scanf("%f", laborSqrYard);
    printf("Floor preperation cost: ");
    scanf("%f", prepCost);
    printf("Discount percent: ");
    scanf("%f", discount);

    return;
}


/*
====================================================
    This function calculates the carpet and labor cost,
    and the discount amount.
       Pre :  sqrYards, costSqrYard, laborSqrYard, prepCost, discount
       Post:  carpetCost, laborCost, discountAmount
====================================================
*/
void calcCosts (int    sqrYards,
                float  costSqrYard,
                float  laborSqrYard,
                float  prepCost,
                float  discount,
                float* carpetCost,
                float* laborCost,
                float* discountAmount)
{
//    Local Declarations
    
    
//    Statements
    *carpetCost = sqrYards *  costSqrYard;
    *laborCost =  sqrYards *  laborSqrYard;
    *discountAmount = (*carpetCost) * (discount/100);


    return;
}// calcCosts

/*
====================================================
    This function calculates the tax on carpet and
    charge to customer.
       Pre : carpetCost, laborCost,discountAmount, 
       Post: charge_customer
====================================================
*/
void calcCharge (float carpetCost,
                 float laborCost,
                 float discountAmount,
                 float* tax_carpet,
                 float* charge_customer)
{
                  // parameter list must be written

//    Local Declarations
    float cost;
    float tax;
//    Statements
    cost = carpetCost - discountAmount + laborCost;
    tax  = cost * TAX_PERCENT / 100;

    *charge_customer = cost + tax;
    *tax_carpet = tax;
    return;
}// calcCharge

/*
====================================================
    This function prints the header of the bill
       Pre : nothing
       Post: header printed
====================================================
*/
void printHeader (void)
{
//    Local Declarations

//    Statements
    printf("Best Carpet Store\n");
    printf("     Invoice     \n\n\n");

    return;
}// printHeader

/*
====================================================
    This function prints the first part of the bill.
       Pre : custNo,       sqrYards, costSqrYard,
             laborSqrYard, prepCost, discount
       Post: all of the above printed
====================================================
*/
void printCarpetData (int custNo,
                      int sqrYards,
                      float costSqrYard,
                      float laborSqrYard,
                      float prepCost,
                      float discount) // parameter list must be written
{
//    Local Declarations
    

//    Statements
    printf("Customer Number:          %d\n\n", custNo);
    printf("Square Yards purchased:   %d\n", sqrYards);
    printf("Cost per square yard  :   %5.2f\n",   costSqrYard);
    printf("Labor per square yard :   %5.2f\n",   laborSqrYard);
    printf("Floor preperation cost:   %5.2f\n\n", prepCost);
    
    return;
}// printCarpetData

/*
====================================================
    This function prints the second part of the bill.
       Pre : carpetCost, laborCost,
             discount,   discountAmount,
             tax,        tax_carpet,
             charge_customer
       Post: all of the above printed
====================================================
*/
void printResults (float carpetCost,
                   float laborCost,
                   float discount,
                   float discountAmount,
                   float tax_carpet,
                   float charge_customer) // parameter list must be written
{
//    Local Declarations

//    Statements
    printf("carpet cost:   %6.2f\n", carpetCost);
    printf("Labor cost:    %6.2f\n", laborCost);
    printf("Discount:      %f\n", discount);
    printf("Discount on carpet    :   %6.2f\n", discountAmount);
    printf("TAX: 8.5 %%\n");
    printf("Tax on Carpet         :   %6.2f\n", tax_carpet);
    printf("--------------------------------------------------------\n");
    printf("charge to customer    :   %6.2f\n", charge_customer);


    return;
}// printResults

/*
====================================================
    This function prints the footer of the bill
       Pre : nothing
       Post: footer printed
====================================================
*/
void printFooter (void)
{
//    Local Declarations

//    Statements
    printf("Thank you for shopping at our store!\n");
    printf("          Have a nice day           \n");
    printf("                and                 \n");
    printf("            come again!             \n");

    return;
}// printFooter


/*
====================================================
                     OUTPUT
====================================================
*/
